package com.heng.compare;

/**
 * 内部比较
 *
 * @author heng
 * @date 2022年01月27日21:11:02
 */
public class TestComparable {


    public static void main(String[] args) {
        Student s1 = new Student("张三", 22);
        Student s2 = new Student("李四", 18);
        System.out.println(s1.compareTo(s2));

    }


    static class Student implements Comparable {
        public String getName() {
            return name;
        }

        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public Student() {
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        private String name;
        private int age;

        @Override
        public int compareTo(Object o) {
            Student s = (Student) o;
            return this.age - s.getAge();
        }
    }
}
